﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports Exp.DAL
Imports Exp.Model

Public Class Booking_Service
    Public Function GetAll() As List(Of RET_Booking)
        Dim ctx As New EXPEntities
        Dim list = ctx.RET_Booking.ToList()
        Return list
    End Function

    Public Sub Add(ByVal objRET_Booking As RET_Booking, ByVal Unit_ID As Integer, ByVal Remark As String, ByVal Night As Integer, ByVal point As Integer)

        'Dim tran As New TransactionScope

        Dim ctx As New DAL.EXPEntities
        If ctx.Connection.State <> ConnectionState.Open Then ctx.Connection.Open()
        Dim txn = ctx.Connection.BeginTransaction


        Try
            ctx.RET_Booking.AddObject(objRET_Booking)

            ctx.SaveChanges()
            'Dim Booking_ID = ctx.RET_Booking.Where(Function(o) o.Status.ToUpper = "REQUEST" And o.Requester_ID = objRET_Booking.Requester_ID).OrderByDescending(Function(o) o.CreateUser).FirstOrDefault.Booking_ID

            Dim objBookingUnit As New RET_Booking_Unit
            objBookingUnit.Booking_ID = objRET_Booking.Booking_ID
            objBookingUnit.Unit_ID = Unit_ID
            objBookingUnit.CreateDate = objRET_Booking.CreateDate
            objBookingUnit.CreateUser = objRET_Booking.CreateUser
            objBookingUnit.Night = Night
            objBookingUnit.Point = point
            objBookingUnit.Remark = Remark

            ctx.RET_Booking_Unit.AddObject(objBookingUnit)


        Catch ex As Exception

            Throw ex

        End Try

        ctx.SaveChanges()
        txn.Commit()

    End Sub
    Public Sub Delete(ByVal iBooking As Integer)
        Dim ctx As New EXPEntities
        Dim obj = ctx.RET_Booking.Where(Function(o) o.Booking_ID = iBooking).FirstOrDefault()
        ctx.RET_Booking.DeleteObject(obj)
        ctx.SaveChanges()
    End Sub
    'Public Function GetByID(ByVal iBooking As Integer) As RET_Booking
    '    Dim ctx As New EXPEntities
    '    Dim obj = ctx.RET_Booking.Where(Function(o) o.Booking_ID = iBooking).FirstOrDefault()
    '    Return obj
    'End Function

    Public Function GetBookingDetail(ByVal Booking_ID As Integer, ByVal Requester_ID As Integer) As List(Of EXP_Select_RET_Booking_Result)
        Dim ctx As New EXPEntities
        Dim obj = ctx.EXP_Select_RET_Booking(Booking_ID, Requester_ID).OrderByDescending(Function(o) o.Booking_ID).ToList
        Return obj
    End Function

    Public Function GetByRequester_ID(ByVal Requester_ID As Integer) As List(Of RET_Booking)
        Dim ctx As New EXPEntities
        Dim obj = ctx.RET_Booking.Where(Function(o) o.Requester_ID = Requester_ID).ToList
        Return obj
    End Function
    Public Sub Edit(ByVal objRET_booking As RET_Booking)
        Dim ctx As New EXPEntities
        Dim obj = ctx.RET_Booking.Where(Function(o) o.Booking_ID = objRET_booking.Booking_ID).FirstOrDefault()
        obj.ApproveDate = objRET_booking.ApproveDate
        obj.Requester_ID = objRET_booking.Requester_ID
        obj.Remark = objRET_booking.Remark
        obj.StartDate = objRET_booking.StartDate
        obj.EndDate = objRET_booking.EndDate
        obj.Status = objRET_booking.Status
        obj.Approval_ID = objRET_booking.Approval_ID
        obj.ApproveDate = objRET_booking.ApproveDate
        obj.UpdateDate = objRET_booking.UpdateDate
        'obj.CreateDate = objRET_booking.CreateDate 
        obj.CreateUser = objRET_booking.CreateUser
        obj.isActive = objRET_booking.isActive
        ctx.SaveChanges()
    End Sub

    Public Function Search(ByVal BookId As Integer?, ByVal UserName As String, ByVal FName As String, _
                           ByVal LName As String, ByVal SrtDate As DateTime?, ByVal EndDate As DateTime?, _
                           ByVal CountryId As Integer?, ByVal BrandId As Integer?, ByVal ResortId As Integer?, _
                           ByVal Location As String, ByVal Status As String) As List(Of EXP_Get_Booking)
        Dim ctx As New EXPEntities
        Return ctx.EXP_Get_Booking(BookId, UserName, FName, LName, SrtDate, EndDate, CountryId, BrandId, ResortId, Location, Status).ToList
    End Function

    Public Sub UpdateStatus(ByVal BookId As Integer, ByVal Status As String, ByVal UpdateUser As Integer, ByVal RejectReason As String)
        Dim ctx As New EXPEntities
        Dim book As RET_Booking = ctx.RET_Booking.Where(Function(o) o.Booking_ID = BookId).FirstOrDefault()
        If book IsNot Nothing Then
            book.Status = Status
            book.UpdateUser = UpdateUser
            book.UpdateDate = Now
            book.Approval_ID = UpdateUser
            book.ApproveDate = Now
            If Status = "APPROVED" Then
                book.ApproveDate = Now
            Else
                book.ReasonReject = RejectReason
            End If

            ctx.SaveChanges()
        End If
    End Sub

    Public Function IsAvailable(ByVal UnitID As Integer, ByVal StartDate As DateTime, ByVal EndDate As DateTime) As Boolean
        Dim ctx As New EXPEntities

        Dim owner As REM_Owner = ctx.REM_Owner.Where(Function(o) o.ShareStartDate >= StartDate AndAlso o.ShareEndDate <= EndDate).FirstOrDefault()

        If owner IsNot Nothing Then
            Dim book = ctx.RET_Booking.Where(Function(o) o.StartDate >= StartDate AndAlso o.EndDate <= EndDate AndAlso o.RET_Booking_Unit.Where(Function(k) k.Unit_ID = UnitID).FirstOrDefault() IsNot Nothing).FirstOrDefault()

            If book Is Nothing Then
                Return True
            ElseIf book.isActive = False Then
                Return True
            Else
                If book.Status.ToUpper = "REJECT" Then
                    Return True
                End If
            End If
        End If
        Return False
    End Function
End Class
